package com.smedi.ismedi.notification.core.application.query.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.notification.core.application.query.SmsTemplateQueryService;
import com.smedi.ismedi.notification.core.application.query.vo.SmsTemplateVo;
import com.smedi.ismedi.notification.core.domain.aggregatemodel.SmsTemplate;
import com.smedi.ismedi.notification.core.domain.service.SmsTemplateDomainService;
import com.smedi.ismedi.notification.core.infrastructure.enumerate.SmsTemplateStatusEnum;
import com.smedi.ismedi.notification.core.port.adapter.persistence.mybatis.mapper.SmsTemplateMapper;
import com.smedi.ismedi.notification.core.port.service.SmsClientRemoteApiService;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import com.tencentcloudapi.sms.v20190711.models.DescribeSmsTemplateListResponse;
import com.tencentcloudapi.sms.v20190711.models.DescribeTemplateListStatus;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * @Author: xulinjun
 * @Date: 2021-7-12 19:34
 */
@Slf4j
@Service
@AllArgsConstructor
public class SmsTemplateQueryServiceImpl implements SmsTemplateQueryService {
    private final SmsTemplateMapper smsTemplateMapper;

    private final SmsTemplateDomainService smsTemplateDomainService;

    private final SmsClientRemoteApiService smsClientRemoteApiService;

    private final DozerUtil dozerUtil;

    @Override
    public IPage getSmsTemplatePaginationByTemplateName(Long offset, Long pageSize, String templateName) {
        Page<SmsTemplateVo> page = new Page<>(offset, pageSize);
        page.addOrder(new OrderItem().descs("id"));

        IPage<SmsTemplateVo> pagination = smsTemplateMapper.selectInternalAssignmentVoPagination(page, templateName);
        pagination.getRecords().stream().forEach(smsTemplateVo -> {
            smsTemplateVo.setStatusMeaning(smsTemplateVo.getStatus());
        });

        return pagination;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SmsTemplateVo getSmsTemplateBySmsTemplateId(Long smsTemplateId) {
        SmsTemplate basic = smsTemplateDomainService.queryTemplateBySmsTemplateId(smsTemplateId);
        SmsTemplate smsTemplate = new SmsTemplate();

        // 更新状态
        if (SmsTemplateStatusEnum.IN_APPROVAL.getValue().equals(basic.getStatus())) {
            DescribeSmsTemplateListResponse response = smsClientRemoteApiService.getSmsTemplateStatus(new ArrayList<Long>() {{
                add(basic.getTencentTemplateId());
            }});

            for (DescribeTemplateListStatus status : response.getDescribeTemplateStatusSet()) {
                if (status.getTemplateId().equals(basic.getTencentTemplateId())) {
                    smsTemplate = smsTemplateDomainService.updateTemplateStatus(smsTemplateId, status.getStatusCode());
                }
            }
        }

        smsTemplate.setStatusMeaning(SmsTemplateStatusEnum.getName(smsTemplate.getStatus()));
        return dozerUtil.convertor(smsTemplate, SmsTemplateVo.class);
    }
}
